package eureka.ui.book;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import eureka.domain.account.WebsiteUser;
import eureka.domain.book.BookItem;
import eureka.domain.book.BookState;
import eureka.exceptions.DatabaseException;
import eureka.service.book.IBookService;
import eureka.service.book.impl.BookService;
import util.lang.RefValue;
import util.web.page.PageBean;

/**
 * Servlet implementation class ShowBookOnshelf
 */
@WebServlet("/Books/ShowBooksByState")
public class ShowBooksByState extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private IBookService bs = new BookService();
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ShowBooksByState() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		WebsiteUser wuser = (WebsiteUser) session.getAttribute("user");
		
		if (wuser == null) {
			response.sendRedirect(request.getAttribute("contextPath")
					+ "/Account/Login?redirect_url="
					+ URLEncoder.encode(
							request.getAttribute("contextPath")
									+ "/Books/ShowBooksByState?"
									+ request.getQueryString(), "UTF-8"));
			return;
		}
		
		String currentpagestr = request.getParameter("curPage");
		String pagesizestr = request.getParameter("pageSize");
		
		
		long currentpage = 0;
		long pagesize = 0;
		
		try{
			currentpage = Long.parseLong(currentpagestr);
			if(currentpage<=0)
				currentpage = 1;
		}catch(Exception e){
			currentpage =1;
		}
		try{
			pagesize = Long.parseLong(pagesizestr);
			if(pagesize <=0||pagesize >100){
				pagesize = 20;
			}
		}catch(Exception e)
		{
			pagesize =20;
		}
		
		RefValue<Long> count = new RefValue<Long>();
		PageBean<BookItem> bipage = new PageBean<BookItem>();
		Short bookstate = Short.parseShort(request.getParameter("state"));
		bipage.setCurrentPage(currentpage);
		bipage.setPageSize(pagesize);
		
		
		
		List<BookItem> list = null;
		
		
		try {
			list= (List<BookItem>)bs.searchBooks(null, 0,wuser.getUID(),BookState.fromCode(bookstate), 0, 0, count);
		} catch (DatabaseException e) {
			response.sendError(500);
			return ;
		}
		
		bipage.setRecords(list, count.getValue());
		
		String param = "listbook"+ BookState.getStateStr(bookstate)+".jsp";
		request.setAttribute("page", bipage);
		request.getRequestDispatcher(param).forward(request, response);
	}
}
